﻿using System;
using System.ComponentModel;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Pivot;
using System.Windows;
using System.IO.IsolatedStorage;
using System.IO;

namespace StarterWebViewer
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            if (DesignerProperties.IsInDesignTool)
            {
                return;
            }

            PivotViewer _viewer = new PivotViewer();
            _viewer.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            _viewer.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            _viewer.CollectionLoadingFailed += (s, e) =>
                {
                    MessageBox.Show(e.Exception.Message);
                };
            _viewer.ItemActionExecuted += CustomPivotViewer_ItemActionExecuted;
            _viewer.ItemDoubleClicked += CustomPivotViewer_ItemDoubleClicked;
            _viewer.LinkClicked += CustomPivotViewer_LinkClicked;
            _pivotGrid.Children.Add(_viewer);

            this.Loaded += (s, e) =>
            {
                string baseUrl;
                if (!App.Current.IsRunningOutOfBrowser)
                {
                    string url = string.Empty;
                    if (App.Current.Host.InitParams.ContainsKey("cxml"))
                    {
                        url = App.Current.Host.InitParams["cxml"];
                    }
                    else
                    {
                        url = "dz1/index.cxml";
                    }

                    baseUrl = App.Current.Host.Source.AbsoluteUri;
                    baseUrl = baseUrl.Substring(0, baseUrl.LastIndexOf("/") + 1);
                    baseUrl += url;
                    SaveSettings(baseUrl);
                }
                else
                {
                    // Out of Browser
                    baseUrl = LoadSettings();
                }

                _viewer.LoadCollection(baseUrl, string.Empty);

            };
        }

        private const string settingsFile = "settings.txt";

        private void SaveSettings(string baseUrl)
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite();
            if (isf.FileExists(settingsFile))
            {
                isf.DeleteFile(settingsFile);
            }

            StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(settingsFile, FileMode.CreateNew, isf));
            writer.WriteLine(baseUrl);
            writer.Close();
        }

        private string LoadSettings()
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite();
            if (isf.FileExists(settingsFile))
            {
                StreamReader reader = new StreamReader(
                    new IsolatedStorageFileStream(settingsFile, FileMode.Open, isf));
                string baseUrl = reader.ReadLine();
                reader.Close();

                return baseUrl;
            }
            else
            {
                MessageBox.Show("Index-Datei nicht gefunden.");
                return string.Empty;
            }
        }

        void CustomPivotViewer_LinkClicked(object sender, LinkEventArgs e)
        {
            BrowseTo(e.Link);
        }

        private void BrowseTo(Uri uri)
        {
            HtmlPage.Window.Navigate(uri, "_new");
        }

        private void BrowseTo(string url)
        {
            Uri uri = new Uri(url);
            BrowseTo(uri);
        }

        void CustomPivotViewer_ItemDoubleClicked(object sender, ItemEventArgs e)
        {
            //page address
            BrowseTo("about:blank");
        }

        void CustomPivotViewer_ItemActionExecuted(object sender, ItemActionEventArgs e)
        {
            //page address
            BrowseTo("about:blank");
        }

        private void _fullscreen_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Host.Content.IsFullScreen = !App.Current.Host.Content.IsFullScreen;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            BrowseTo("http://win7togo.codeplex.com/releases");
        }

        private void _msdnLogo_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            BrowseTo("http://msdn.microsoft.com/de-de/windows/default.aspx");
        }

        private void _windowsLogo_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            BrowseTo("http://www.microsoft.de/visualstudio");
        }


    }
}
